#!/usr/bin/python3
import pymysql
import requests
import time
from lxml import etree

# 设置请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0'
}
cookies = {
    'cookie': '"108300"; bid=x6CWhyewqig; __utma=30149280.485109852.1574991282.1574991282.1574991282.1; __utmc=30149280; __utmz=30149280.1574991282.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; ap_v=0,6.0; gr_user_id=056ec422-999e-4cc8-82e1-4757d6547298; gr_session_id_22c937bbd8ebd703f2d8e9445f7dfd03=a3df6afd-13f6-4470-b577-a185c4c218e3; gr_cs1_a3df6afd-13f6-4470-b577-a185c4c218e3=user_id%3A0; gr_session_id_22c937bbd8ebd703f2d8e9445f7dfd03_a3df6afd-13f6-4470-b577-a185c4c218e3=true; _vwo_uuid_v2=DEE210CAD2A1C37C8754AAD0465FEFC45|3c7ca2da3525dbfd34f59bad5030fbde; __gads=ID=9438760ce15e89b2:T=1574991108:S=ALNI_MbPmQ5P86tmDdlvMJS_0so-WCfqzQ; __utmb=30149280.4.10.1574991282'}
# 封装url请求方法

page_num = 1


def getcontent(aurl):
    url = aurl
    response = requests.get(url, cookies=cookies, headers=headers).text
    tree = etree.HTML(response)
    x_cont = tree.xpath('//div[@class="entry-content"]/table/tr/td/div')[0]
    # print(x_cont.text)
    for r in x_cont:
        if r.text is None:
            x_cont.remove(r)
            print(r.text)
    content2 = etree.tostring(x_cont, encoding='UTF-8', pretty_print=True, method='html').decode("utf-8")
    content2 = content2.replace('div', 'p')
    print(content2)
    # return content2


    # db = pymysql.connect(host="localhost", user="root", password="root", database="puercn_com")
    # # 使用 cursor() 方法创建一个游标对象 cursor
    # cursor = db.cursor()
    # # 执行SQL语句
    # sql = 'select * from news where status=0 and typeid=1 limit 1'
    # cursor.execute(sql)
    # # 获取所有记录列表
    # results = cursor.fetchall()
    # for row in results:
    #     nid = row[0]
    #     nlink = row[4]

    # cursor.close()
    # db.close()
    # print('sql执行成功')
nlink = 'https://www.puercn.com/baicha/bcjg/229875.html'
c = getcontent(nlink)
# c.replace('div', 'p')
# print(c)
# for i in c:
#     print(i)


# for i in range(1, range_n):
#     args = getlist(str(i), typeid, url)
#     db = pymysql.connect(host="localhost", user="root", password="root", database="puercn_com")
#     # 使用 cursor() 方法创建一个游标对象 cursor
#     cursor = db.cursor()
#     sql = "insert into news(title,link,typeid) values(%s,%s,%s)"
#     insert = cursor.executemany(sql, args)
#     print('批量插入返回受影响的行数：', insert)
#     cursor.close()
#     db.commit()
#     db.close()
#     print('sql执行成功')
#     print(i)
#     time.sleep(10)
#
# print('typeid='+str(typeid)+'-success')
